import CarList from "./carData";

/**
 * 随机获取一个车辆
 * @returns 车辆数据
 */
function getRandomCar() {
  const randomIndex = Math.floor(Math.random() * CarList.length);
  return CarList[randomIndex];
}
/**
 * 随机获取分布月份
 * @param year 年
 * @param month 月
 * @param count 数量
 * @returns 返回月份数量
 */
function getDatesByMonthAndYear(year: number, month: number, count: number) {
  const dates = [];
  const daysInMonth = new Date(year, month, 0).getDate();

  for (let i = 0; i < count; i++) {
    const randomDay = Math.floor(Math.random() * daysInMonth) + 1;
    const selectedDate = new Date(year, month - 1, randomDay);
    dates.push(selectedDate.toISOString().split("T")[0]);
  }

  return dates;
}



  // const year = 2022; // 输入年份，这里假设为2022年
  // const month = 8; // 输入月份，这里假设为8月
  // const count = 5; // 输入数量，这里假设为5个日期

  // const result = getDatesByMonthAndYear(year, month, count);
  // console.log(result);
/**
 * 切割数量
 * @param total 总数
 * @param maxPerGroup 最大数量
 * @returns
 */
// function splitTotal(total: number, maxPerGroup: number) {
//   const result = [];
//   let remaining = total;

//   while (remaining > 0) {
//     const groupSize = Math.min(remaining, maxPerGroup);
//     result.push(groupSize);
//     remaining -= groupSize;
//   }

//   return result;
// }

function splitTotal(total: number, maxPerGroup: number[]) {
  if(total<=0){
    return
  }
  const result = [];
  let remaining = total;

  while (remaining > 0) {
    const max = maxPerGroup[Math.floor(Math.random() * maxPerGroup.length)];
    const groupSize = Math.min(remaining, max);
    result.push(groupSize);
    remaining -= groupSize;
  }

  return result;
}

//   const total = 20; // 输入总数，这里假设为20
//   const maxPerGroup = 6; // 输入每份最大数，这里假设为6

//   const result = splitTotal(total, maxPerGroup);
//   console.log(result);
/**
 * 
 * @param options 
 * @returns 
 */
function selectRandom<T>(options: T[]): T {
  if (options.length === 0) {
    throw new Error("选项列表不能为空");
  }
  const randomIndex = Math.floor(Math.random() * options.length);
  console.log("随机选一个：",randomIndex,options[randomIndex]);
  
  return options[randomIndex];
}
function selectRandom1() {

  let result = Math.random()*7000+7000
  return result;
}

function splitNumberRandomly(number, min, max) {
  if (number === 0) {
      return [];
  }

  const random = Math.floor(Math.random() * (max - min + 1)) + min;
  const nextNumber = number - random;

  return [random, ...splitNumberRandomly(nextNumber, min, max)];
}
export { getRandomCar, getDatesByMonthAndYear, splitTotal ,selectRandom,selectRandom1,splitNumberRandomly};
